package com.controller.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.Identity;
import com.po.Profession;
import com.po.User;
import com.po.UserRegister;
import com.service.manage.AdminService;
import com.service.manage.IdentityService;
import com.service.manage.ProfessionService;
import com.service.manage.UserRegisterService;
import com.service.manage.UserRoleService;
import com.service.manage.UserService;
import com.service.manage.UserZJService;

@Controller
@RequestMapping("/jsp/manager")
public class UserController {
    @Resource(name = "userServiceImp")
    private UserService userService;
    @Resource(name = "userRegisterServiceImp")
    private UserRegisterService userRegisterService;
    @Resource(name = "userRoleServiceImp")
    private UserRoleService userRoleService;
    @Resource(name = "professionServiceImp")
    private ProfessionService professionService;
    @Resource(name = "userZJServiceImp")
    private UserZJService userZJService;
    @Resource(name = "identityServiceImp")
    private IdentityService identityService;
    @Resource(name = "adminServiceImp")
    public AdminService adminService;

    @ResponseBody
    @RequestMapping(value = "/getIdentityList")
    public List<Identity> getIdentityList() {
	List<Identity> list = identityService.getIdentityList();
	return list;
    }

    @ResponseBody
    @RequestMapping(value = "/getProfessionList")
    public List<Profession> getProfessionList() {
	List<Profession> list = professionService.getProfessionList();
	return list;
    }

    @ResponseBody
    @RequestMapping(value = "/getUserList")
    public Map<String, Object> getUserList(User user, Integer page, Integer rows) {
	user.setPage(((page - 1) * rows));
	user.setRows(rows);
	System.out.println(user.getPage() + " " + user.getRows());
	List<User> list = userService.getUserList(user);
	int count = userService.getUserCount(user);
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("rows", list);
	map.put("total", count);
	return map;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteUser")
    public int deleteUser(@RequestParam("user_id") String userid) {
	int i = userRegisterService.deleteUserRegister(userid);
	return i;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteUserList")
    public int delUserList(String userids) {
	String[] userid = userids.split(",");
	int result = userRegisterService.deleteUserRegisterList(userid);
	return result;
    }

    @ResponseBody
    @RequestMapping(value = "/addUserIsOk")
    public int addUserIsOk(String user_id) {
	UserRegister userRegister = new UserRegister();
	userRegister.setUser_id(user_id);
	// 设置用户有效
	userRegister.setUser_validate("1");
	UserRegister userRegisterr = userRegisterService.addUserRegisterIsOk(userRegister);
	if (userRegisterr == null)
	    return 1;
	else
	    return 0;
    }

    @ResponseBody
    @RequestMapping(value = "/addUser")
    public int addUser(User user) {
	user.setUser_remark(user.getUser_remark().replace(" ", "&nbsp").replace("\r\n", "<br>"));
	int i = userService.addUser(user);
	return i;
    }

    @ResponseBody
    @RequestMapping(value = "/updateUser")
    public int updateUser(User user) {
	user.setUser_remark(user.getUser_remark().replace(" ", "&nbsp").replace("\r\n", "<br>"));
	int i = userService.updateUser(user);
	return i;
    }
}
